#!bin/bash
RETVAL=0
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
shell_home=/home/shell
rocketmq_version=rocketmq-4.8.0
## 下载RocketMQ编译结果, 这里用最新版4.8.0
download_rocketmq()
{
    cd $shell_home
    rocketmq_home=$shell_home/$rocketmq_version
    wget https://handson.oss-cn-shanghai.aliyuncs.com/$rocketmq_version.tar.gz --no-check-certificate
    tar -zxvf $rocketmq_version.tar.gz && cd $rocketmq_version && ls -l
    echo '-------RocketMQ download success-------' `date '+%Y-%m-%d %H:%M:%S'`
}

## 安装Namesrv服务
install_namesrv()
{
    rocketmq_home=$shell_home/$rocketmq_version

    cd $rocketmq_home/conf/ && touch kvConfig.properties
    wget https://code.aliyun.com/handsonlabs_rocketmq/lab_images/raw/master/rmq/namesrv.conf --no-check-certificate  ## 下载Namesrv配置

    cd $rocketmq_home
    sed -i 's/Xms4g -Xmx4g -Xmn2g/Xms300m -Xmx300m -Xmn200m/g' ./bin/runserver.sh ## 由于实验环境配置不高, 需要修改jvm参数

    nohup ./bin/mqnamesrv -c ./conf/namesrv.conf > /dev/null 2>&1 &
    echo '-------Namesrv boot success-------' `date '+%Y-%m-%d %H:%M:%S'`
}

## 安装Broker服务
install_broker()
{
    rocketmq_home=$shell_home/$rocketmq_version

    cd $rocketmq_home/conf
    wget https://code.aliyun.com/handsonlabs_rocketmq/lab_images/raw/master/rmq/async_broker.conf --no-check-certificate ## 下载Broker配置

    mkdir -p /data/rocketmq/commitlog
    mkdir -p /data/rocketmq/consumequeue

    cd $rocketmq_home
    sed -i 's/Xms8g -Xmx8g -Xmn4g/Xms300m -Xmx300m -Xmn200m/g' ./bin/runbroker.sh ## 由于实验环境配置不高, 需要修改jvm参数

    nohup sh ./bin/mqbroker -c ./conf/async_broker.conf > /dev/null 2>&1 & ## 启动Broker服务
    echo '-------Broker boot success-------' `date '+%Y-%m-%d %H:%M:%S'`
}

## 安装带ACL的Broker服务
install_broker_acl()
{
    rocketmq_home=$shell_home/$rocketmq_version

    cd $rocketmq_home/conf
    wget https://code.aliyun.com/handsonlabs_rocketmq/lab_images/raw/master/rmq/async_broker_acl.conf --no-check-certificate ## 下载带ACL配置的Broker配置文件

    mkdir -p /data/rocketmq/commitlog
    mkdir -p /data/rocketmq/consumequeue

    cd $rocketmq_home
    sed -i 's/Xms8g -Xmx8g -Xmn4g/Xms300m -Xmx300m -Xmn200m/g' ./bin/runbroker.sh ## 由于实验环境配置不高, 需要修改jvm参数

    nohup sh ./bin/mqbroker -c ./conf/async_broker_acl.conf > /dev/null 2>&1 & ## 启动Broker服务
    echo '-------Broker With ACL boot success-------' `date '+%Y-%m-%d %H:%M:%S'`
}

# 下载RocketMQ Console包
download_rocketmq_console()
{
    cd ~/
    wget https://handson.oss-cn-shanghai.aliyuncs.com/rocketmq-console-ng-2.0.0.jar
}

# 安装一个普通的RocketMQ Console, 不支持ACL的
install_rocketmq_console()
{
    cd ~/ && nohup java -jar rocketmq-console-ng-2.0.0.jar -Xms300m -Xmx300m -Xmn300m --server.port=60000 > /dev/null 2>&1 &
}

# 安装一个支持ACL的RocketMQ Console
install_rocketmq_console_acl()
{
    cd ~/ && nohup java -jar rocketmq-console-ng-2.0.0.jar -Xms300m -Xmx300m -Xmn300m --rocketmq.config.accessKey=rocketmq2 --rocketmq.config.secretKey=12345678 --server.port=60000 > /dev/null 2>&1 &
}

# 安装一个新的broker, namesrv和当前的一样
install_new_broker()
{
    mkdir -p ~/data1/rocketmq
    mkdir -p ~/data1/rocketmq/commitlog
    mkdir -p ~/data1/rocketmq/consumequeue

    cd ~/ && mkdir $rocketmq_version.2 && cd $rocketmq_version.2
    cp ~/$rocketmq_version.tar.gz ./ && tar -zxvf $rocketmq_version.tar.gz && cd $rocketmq_version && ls -l
    export BROKER2_HOME=${PWD}

    cd $BROKER2_HOME
    mkdir ~/logs.2 -p
    sed -i 's/logs\/rocketmqlogs/logs.2\/rocketmqlogs/g' ./conf/logback_broker.xml ## 第二个broker log路径需要不一样

    cd $BROKER2_HOME/conf && wget https://code.aliyun.com/handsonlabs_rocketmq/lab_images/raw/master/rmq/async_broker2.conf --no-check-certificate

    cd $BROKER2_HOME
    sed -i 's/Xms8g -Xmx8g -Xmn4g/Xms300m -Xmx300m -Xmn200m/g' ./bin/runbroker.sh ## 由于实验环境配置不高, 需要修改jvm参数

    nohup sh ./bin/mqbroker -c ./conf/async_broker2.conf > /dev/null 2>&1 & ## 启动Broker服务
    echo '-------New Broker boot success-------' `date '+%Y-%m-%d %H:%M:%S'`

}
COMMAND=$1
case $COMMAND in
  new_broker)
    install_new_broker
    echo '已经成功安装一个新的Broker'
    ;;
  quick)
    download_rocketmq
    install_namesrv
    install_broker
    echo '已经成功安装一个简单的RocketMQ集群(1个Namesrv + 1个Broker), 脚本如何执行的, 可以打开脚本看注释.'
    ;;
  quick_acl)
    download_rocketmq
    install_namesrv
    install_broker_acl
    echo '已经成功安装一个简单的RocketMQ集群(1个Namesrv + 1个Broker), 脚本如何执行的, 可以打开脚本看注释.'
    ;;
  console)
    download_rocketmq_console
    install_rocketmq_console
    echo '已经成功安装一个RocketMQ Console'
    ;;
  console_acl)
    download_rocketmq_console
    install_rocketmq_console_acl
    echo '已经成功安装一个RocketMQ Console'
    ;;
  *)
    ;;
esac




exit $RETVAL